package com.one.controller;

import com.one.annotation.RequestUser;
import com.one.service.ProductService;
import com.one.util.PageUtil;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.vo.CommentVo;
import com.one.vo.ProductVo;
import com.one.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName: ProductController
 * @Auther: yph
 * @Date: 2022/1/19 09:05
 * @Description: 商品Controller层
 */
@Slf4j
@Api(tags = "I.商品详情操作")
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * @Author: yph
     * @Date: 2022/1/19 09:07
     * @return:
     * @Description: 查看商品详情，根据商品id
     */
    @ApiImplicitParam(name = "id", value = "商品id")
    @ApiOperation(value = "查询商品详情", notes = "需要传入商品的id，根据id进行查询")
    @GetMapping("/queryProductInfo")
    public RequestReturn<ProductVo> getProductById(@ApiIgnore @RequestUser UserVo userVo,
                                                   @RequestParam("id") Integer id,@ApiIgnore HttpSession session) {

        return productService.queryProductById(Objects.isNull(userVo) ? null : userVo.getUserId(), id, session.getId());
    }

    /**
     * @Author: yph
     * @Date: 2022/1/14 18:35
     * @return:
     * @Description: 根据商品和是查询条件--- 查询商品评价
     */
    @ApiOperation(value = "查询商品主评论", notes = "可以根据是否嗮图进行查询，支持分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", required = true, value = "商品id"),
            @ApiImplicitParam(name = "withPicture", required = false, value = "传值就是嗮图，为空不嗮图 "),
            @ApiImplicitParam(name = "pageNum", required = false, value = "当前页"),
            @ApiImplicitParam(name = "pageSize", required = false, value = "每页容量")
    })
    @GetMapping("/queryMainComments")
    public RequestReturn<PageUtil<List<CommentVo>>> queryCommentByProductId(@RequestParam("productId") Integer productId,
                                                                            @RequestParam(value = "withPicture", required = false) String withPicture,
                                                                            @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                                            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        return productService.queryCommentByProductId(productId, withPicture, pageNum, pageSize);
    }

    /**
     * @Author: yph
     * @Date: 2022/1/14 18:35
     * @return:
     * @Description: 根据商品和是查询条件--- 查询商品评价
     */
    @ApiOperation(value = "查询商品子评论", notes = "根据评论id 查看所有当前评论下的子评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", required = true, value = "主评论id")
    })
    @GetMapping("/querySubComments")
    public RequestReturn<List<CommentVo>> queryCommentByProductId(@RequestParam("parentId") Integer parentId) {
        return productService.queryCommentByParentId(parentId);
    }


}
